<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" :rules="rules" ref="params" class="demo-form-inline" label-position="top">
      <el-row :gutter="36">
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.invoiceTitle')" prop="header">
            <el-input
              v-model="params.header"
              :placeholder="$t('config.invoiceInformation.invoiceTitlePlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.ird')" prop="taxId">
            <el-input
              v-model="params.taxId"
              :placeholder="$t('config.invoiceInformation.irdPlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.registerPhone')" prop="registrationPhone">
            <el-input
              v-model="params.registrationPhone"
              :placeholder="$t('config.invoiceInformation.registerPhone')"
              clearable
              maxlength="11"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.bankName')" prop="openingBankName">
            <el-input
              v-model="params.openingBankName"
              :placeholder="$t('config.invoiceInformation.bankNamePlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.bankAccount')" prop="openingBankAccount">
            <el-input
              v-model="params.openingBankAccount"
              :placeholder="$t('config.invoiceInformation.bankAccountPlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.invoiceInformation.registerAddress')" prop="registrationAddress">
            <el-input
              v-model="params.registrationAddress"
              :placeholder="$t('config.invoiceInformation.registerAddressPlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button
        type="primary"
        @click="
          loading = true
          handleSubmit()
        "
        :loading="loading"
      >
        {{ $t('common.save') }}
      </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle, checkMobilePhone, checkFixPhone } from '@package/utils'
  import { EnumApiCode } from '../../../../../../globalEnum'
  import { apiPostAddInvoiceDict, apiPutEditInvoiceDict, apiGetInvoiceDict } from '../api/invoiceInformation.js'

  export default {
    name: 'InvocieInformationDialog',
    data() {
      return {
        visible: false,
        loading: false,
        title: this.$t('config.invoiceInformation.add'),
        params: {
          header: '',
          taxId: '',
          registrationPhone: '',
          openingBankName: '',
          openingBankAccount: '',
          registrationAddress: ''
        },
        rules: {
          header: [
            { required: true, message: this.$t('config.invoiceInformation.invoiceTitlePlaceholder'), trigger: 'blur' }
          ],
          taxId: [{ required: true, message: this.$t('config.invoiceInformation.irdPlaceholder'), trigger: 'blur' }],
          registrationPhone: [
            { required: true, message: this.$t('config.invoiceInformation.registerPhonePlaceholder'), trigger: 'blur' },
            { validator: this.validatePhone, trigger: 'blur' }
          ],
          openingBankName: [
            { required: true, message: this.$t('config.invoiceInformation.bankNamePlaceholder'), trigger: 'blur' }
          ],
          openingBankAccount: [
            { required: true, message: this.$t('config.invoiceInformation.bankAccountPlaceholder'), trigger: 'blur' }
          ],
          registrationAddress: [
            {
              required: true,
              message: this.$t('config.invoiceInformation.registerAddressPlaceholder'),
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.title = this.$t('config.invoiceInformation.edit')
          apiGetInvoiceDict(info?.id).then(({ data }) => {
            this.params = Object.assign(this.params, cloneDeep(data))
          })
          return
        }
        this.title = this.$t('config.invoiceInformation.add')
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params
            }
            try {
              const { code, msg } = this.params.id
                ? await apiPutEditInvoiceDict(param)
                : await apiPostAddInvoiceDict(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
                this.handleClose()
                this.$emit('handleSuccess')
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            this.loading = false
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.loading = false
        this.visible = false
      },
      validatePhone(rule, value, callback) {
        if (value && value !== '' && !checkMobilePhone(value) && !checkFixPhone(value)) {
          callback(new Error(this.$t('config.invoiceInformation.registerRightPhonePlaceholder')))
        } else {
          callback()
        }
      }
    }
  }
</script>
